

import java.awt.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;




public class ManagePerson {
    PersonBean[] sBeans=new PersonBean[200];//数组添加位置
    Scanner scanner=new Scanner(System.in);
	//显示所有英雄
	public void showALL() {
		ArrayList<PersonBean> aList=new ArrayList<PersonBean>();
		String sqlString="select pid,pname,sex,type,addtime from person";
		QueryDataResult rSet=DBManager.excuteQuery(sqlString);
			if (rSet!=null) {
				try {
					ResultSet rsSet=rSet.rs;
					while (rsSet.next()) {
						aList.add(new PersonBean(rsSet.getInt("pid"),rsSet.getString("pname"),rsSet.getString("sex"),
								rsSet.getString("type"),rsSet.getDate("addtime")));
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			rSet.close();
			System.out.println("英雄编号\t英雄姓名\t英雄性别\t英雄位置\t加入时间");
			for (PersonBean pBean : aList) {
				System.out.println(pBean.getId()+"\t"+pBean.getNameString()+"\t"+pBean.getSexString()
				+"\t"+pBean.getTypeString()+"\t"+pBean.getAddTimeDate());
			}
		}
	//判断pid是否存在
	public PersonBean selectById(int pid) {
		String sql="select pid,pname,sex,type,addtime from person where pid="+pid;
	    QueryDataResult queryDataResult =DBManager.excuteQuery(sql);
	    try {
	    	ResultSet rsSet=queryDataResult.rs;
			if (rsSet.next()) {
				//System.out.println("pid重复");
				return new PersonBean(rsSet.getInt("pid"),rsSet.getString("pname"),rsSet.getString("sex"),
						rsSet.getString("type"),rsSet.getDate("addtime"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	    return null;
	}
	
	//添加
	public void addPerson() {
		System.out.println("英雄编号");
	    int pid=scanner.nextInt();
	    PersonBean pBean=selectById(pid);
	    if (pBean!=null) {
			System.out.println("英雄编号重复,请重新添加");
		}else {
			    System.out.println("英雄姓名");
			    String pname=scanner.next();
			    System.out.println("英雄性别");
			    String sex=scanner.next();
			    System.out.println("英雄位置");
			    String type=scanner.next();
			    System.out.println("时间 yyyy-MM-dd");
			    String addtime=scanner.next();
			    String sqlString="insert into person(pid,pname,sex,type,addtime) values('"+pid+"','"+pname+"','"+sex+"','"+type+"','"+addtime+"')";
			    int result=DBManager.excuteUpdate(sqlString);
			    System.out.println(result>0?"添加成功":"添加失败");
		}
	    
	}
	//修改
	public void personUpdate() {
		System.out.println("请输入要改的英雄编号");
		int pid=scanner.nextInt();
		PersonBean pBean=selectById(pid);
		if (pBean==null) {
			System.out.println("英雄编号不存在,无法修改");
		}else {
			String pname=pBean.getNameString();
			String sex=pBean.getSexString();
			String type=pBean.getTypeString();
			String addtime=pBean.getAddTimeDate()+"";
			System.out.println("是否修改姓名 y/n");
			String str=scanner.next();
			if (str.equals("y")) {
				System.out.println("请输入需要修改的英雄姓名");
				pname=scanner.next();
			}
			System.out.println("是否修改性别 y/n");
			str=scanner.next();
			if (str.equals("y")) {
				System.out.println("请输入需要修改的英雄性别");
				sex=scanner.next();
			}
			System.out.println("是否修改位置 y/n");
			str=scanner.next();
			if (str.equals("y")) {
				System.out.println("请输入需要修改的英雄位置");
				type=scanner.next();
			}
			System.out.println("是否修改加入时间 y/n");
			str=scanner.next();
			if (str.equals("y")) {
				System.out.println("请输入需要修改的加入时间");
				addtime=scanner.next();
			}
			String sqlString="UPDATE person SET pname='"+pname+"',sex='"+sex+"',type='"+type+"'"
					+ ",addtime='"+addtime+"' WHERE pid='"+pid+"'";
			int result=DBManager.excuteUpdate(sqlString);
			System.out.println(result>0?"修改成功":"修改失败");
		}
		
	}
	public void personUpdate1() {
		System.out.println("请输入要改的英雄编号");
		int pid=scanner.nextInt();
		PersonBean pBean=selectById(pid);
		if (pBean==null) {
			System.out.println("英雄编号不存在,无法修改");
		}else {
			String pname=pBean.getNameString();
			String sex=pBean.getSexString();
			String type=pBean.getTypeString();
			String addtime=pBean.getAddTimeDate()+"";
			String sqlString="UPDATE person SET pid='"+pid+"'";
			System.out.println("是否修改姓名 y/n");
			String str=scanner.next();
			if (str.equals("y")) {
				System.out.println("请输入需要修改的英雄姓名");
				pname=scanner.next();
				sqlString=sqlString+",pname='"+pname+"'";
			}
			System.out.println("是否修改性别 y/n");
			str=scanner.next();
			if (str.equals("y")) {
				System.out.println("请输入需要修改的英雄性别");
				sqlString=sqlString+",sex='"+scanner.next()+"'";
			}
			System.out.println("是否修改位置 y/n");
			str=scanner.next();
			if (str.equals("y")) {
				System.out.println("请输入需要修改的英雄位置");
				sqlString=sqlString+",type='"+scanner.next()+"'";
			}
			System.out.println("是否修改加入时间 y/n");
			str=scanner.next();
			if (str.equals("y")) {
				System.out.println("请输入需要修改的加入时间");
				sqlString=sqlString+",addtime='"+scanner.next()+"'";
			}
			sqlString=sqlString+"where pid="+pid;
			int result=DBManager.excuteUpdate(sqlString);
			System.out.println(result>0?"修改成功":"修改失败");
		}
		
	}
	
	//判断pname是否存在
		public PersonBean selectById1(String pname) {
			String sql="select pid,pname,sex,type,addtime from person where pname='"+pname+"'";
		    QueryDataResult queryDataResult =DBManager.excuteQuery(sql);
		    try {
		    	ResultSet rsSet=queryDataResult.rs;
				if (rsSet.next()) {
					return new PersonBean(rsSet.getInt("pid"),rsSet.getString("pname"),rsSet.getString("sex"),
							rsSet.getString("type"),rsSet.getDate("addtime"));
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		    return null;
		}
	
	//删除
	public void deletePerson() {
		System.out.println("请输入要删除的英雄编号");
		int pid=scanner.nextInt();
		PersonBean pBean=selectById(pid);
	    if (pBean==null) {
			System.out.println("英雄编号不存在,无法删除");
		}else {
			String sqlString="DELETE from person WHERE pid='"+pid+"'";
			int result=DBManager.excuteUpdate(sqlString);
			System.out.println(result>0?"删除成功":"删除失败");
		}
		}
	
	public void deletePerson1() {
		System.out.println("请输入要删除的英雄姓名");
		String pname=scanner.next();
		PersonBean pBean=selectById1(pname);
	    if (pBean==null) {
			System.out.println("此英雄不存在,无法删除");
		}else {
			String sqlString="DELETE from person WHERE pname='"+pname+"'";
			int result=DBManager.excuteUpdate(sqlString);
			System.out.println(result>0?"删除成功":"删除失败");
		}
		}
	
	//模糊查询
	public void fuzzyQuery() {
		ArrayList<PersonBean> aList=new ArrayList<PersonBean>();
		System.out.println("请输入英雄姓名/姓氏/名");
		String pname=scanner.next();
		System.out.println("正在为你查询。。。");
		String sql="select pid,pname,sex,type,addtime from person where pname"+" like '%"+pname+"%'";
	    QueryDataResult queryDataResult =DBManager.excuteQuery(sql);
	    try {
	    	ResultSet rsSet=queryDataResult.rs;
	    	while (rsSet.next()) {
				aList.add(new PersonBean(rsSet.getInt("pid"),rsSet.getString("pname"),rsSet.getString("sex"),
						rsSet.getString("type"),rsSet.getDate("addtime")));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	    queryDataResult.close();
	    if (aList.size()==0) {
			System.out.println("对不起，未查询到相关内容!");
			return;
		}
	    System.out.println("英雄编号\t英雄姓名\t英雄性别\t英雄位置\t加入时间");
	    for (PersonBean pBean : aList) {
	    	System.out.println(pBean.getId()+"\t"+pBean.getNameString()+"\t"+pBean.getSexString()
			+"\t"+pBean.getTypeString()+"\t"+pBean.getAddTimeDate());
		}
		}
	//对象排序
	public void objectsort() {
			System.out.println("-----欢迎进入按年龄排序功能 - - - - - -");
			QueryDataResult qdr=DBManager.excuteQuery("select pid,pname,sex,type,addtime from person");
			ArrayList<PersonBean> aList=new ArrayList<PersonBean>();
			try {
		    	ResultSet rsSet=qdr.rs;
		    	while (rsSet.next()) {
					aList.add(new PersonBean(rsSet.getInt("pid"),rsSet.getString("pname"),rsSet.getString("sex"),
							rsSet.getString("type"),rsSet.getDate("addtime")));
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			qdr.close();
			System.out.println("英雄编号\t英雄姓名\t英雄性别\t英雄位置\t年龄\t加入时间");
		    for (PersonBean pBean : aList) {
		    	System.out.println(pBean.getId()+"\t"+pBean.getNameString()+"\t"+pBean.getSexString()
				+"\t"+pBean.getTypeString()+"\t"+pBean.getYear()+"\t"+pBean.getAddTimeDate());
			}
			//把英雄放入数组
			PersonBean[] sBeans=new PersonBean[aList.size()];
			for (int i = 0; i < sBeans.length; i++) {
				sBeans[i]=aList.get(i);
			}
			//冒泡排序降序
			for (int i = 0; i < sBeans.length-1; i++) {
				for (int j = 0; j < sBeans.length-1-i; j++) {
					if (sBeans[j].getYear() < sBeans[j+1].getYear()) {
						PersonBean e = sBeans[j];
						sBeans[j] = sBeans[j+1];
						sBeans[j+1] = e;
					}
				}
			}
			System.out.println("排序后的结果为:");
			for (PersonBean pBean : sBeans) {
				System.out.println(pBean.getId()+"\t"+pBean.getNameString()+"\t"+pBean.getSexString()
				+"\t"+pBean.getTypeString()+"\t"+pBean.getYear()+"\t"+pBean.getAddTimeDate());
			}
		}
}
